package ${package.ServiceImpl};

import com.dbcloud.lxdb.modules.device.model.po.${entity}PO;
import com.dbcloud.lxdb.modules.device.model.dto.${entity}DTO;
import com.dbcloud.lxdb.modules.device.model.vo.${entity}PageVO;
import com.dbcloud.lxdb.common.mybatis.util.PageUtil;
import com.dbcloud.lxdb.common.core.exception.Asserts;
<#assign myDateType = ["LocalDateTime","LocalDate"]/>
import ${package.Mapper}.${entity}Mapper;
import ${package.Service}.${entity}Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.lang.Assert;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.dbcloud.lxdb.common.mybatis.constant.DataSourceConstants;

import java.util.List;


/**
 * ${table.comment!}实现层
 *
 * @author ${author}
 */
@Slf4j
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}PO> implements ${table.serviceName} {

    @Override
    public Page<${table.entityName}PageVO> page(Page<${table.entityName}PO> page, ${table.entityName}DTO dto) {
        // 设置查询条件并按创建时间倒序
        LambdaQueryWrapper<${table.entityName}PO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(dto).orderByDesc(${table.entityName}PO::getGmtCreate);
        // 获取数据后，转VO返回
        Page<${table.entityName}PO> pagePo = baseMapper.selectPage(page, queryWrapper);
        return PageUtil.getPageVO(pagePo, BeanUtil.copyToList(pagePo.getRecords(), ${table.entityName}PageVO.class));
    }

    @Override
    public List<${table.entityName}DTO> list(${table.entityName}DTO dto) {
        LambdaQueryWrapper<${table.entityName}PO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(dto).orderByDesc(${table.entityName}PO::getGmtCreate);
        List<${table.entityName}PO> poList = baseMapper.selectList(queryWrapper);
        return BeanUtil.copyToList(poList, ${table.entityName}DTO.class);
    }

    @Override
    public void insert(${table.entityName}DTO dto) {
        int insertNum = baseMapper.insert(dto);
        Assert.isTrue(insertNum == 1, () -> new ApiException("新增${table.comment!}失败"));
    }

    @Override
    public void updateById(${table.entityName}DTO dto) {
        int updateNum = baseMapper.updateById(dto);
        Assert.isTrue(updateNum == 1, () -> new ApiException("修改${table.comment!}失败"));
    }

    @Override
    public ${table.entityName}DTO getById(Integer id) {
        ${table.entityName}PO po = baseMapper.selectById(id);
        return po == null ? null : BeanUtil.copyProperties(po, ${table.entityName}DTO.class);
    }

}
</#if>
